package com.imau.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imau.bean.Role;
import com.imau.common.dataSourceTool.SqlSessionFactoryUtil;
import com.imau.common.entiry.PageQuery;
import com.imau.common.utils.StringUtils;
import com.imau.mapper.RoleMapper;
import com.imau.service.RoleService;
import org.apache.ibatis.session.SqlSession;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

public class RoleServiceImp extends ServiceImpl<RoleMapper, Role> implements RoleService {

    //操作数据库的session
    private SqlSession session;

    public RoleServiceImp()
    {
        //父类 ServiceImpl属性，自动注入
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        this.baseMapper = session.getMapper(RoleMapper.class);
    }
    @Override
    public Role queryById(Serializable id) {

        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);

        return this.baseMapper.selectById(id); //super.getById(id);
    }

    @Override
    public List<Role> queryList(Role entity) {
        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        //1.构建查询条件
        LambdaQueryWrapper<Role> lqw = buildQueryWrapper(entity);
        //2.调用方法
        return super.list(lqw);
    }

    @Override
    public Page<Role> queryPageList(Role entity, PageQuery pageQuery) {
        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        //1.构建查询条件
        LambdaQueryWrapper<Role> lqw = buildQueryWrapper(entity);


        return super.page(pageQuery.build(),lqw);
    }

    @Override
    public boolean updateByEntity(Role entity) {
        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return this.updateById(entity);
    }

    @Override
    public boolean saveEntity(Role entity) {
        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return super.save(entity);
    }

    @Override
    public boolean removeWithById(Serializable id) {

        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);

        //1.删除角色
        boolean b = this.removeById(id);
        //2.删除中间表
        int row = this.baseMapper.deleteUserRoleByI(id);

        return  row >=0&b;
    }

    @Override
    public boolean removeWithValidByIds(Collection<?> ids) {
        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return  this.removeBatchByIds(ids);
    }

    @Override
    public LambdaQueryWrapper<Role> buildQueryWrapper(Role bo) {
        //根据字段类别
        LambdaQueryWrapper<Role> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Role::getName, bo.getName());
        lqw.orderByAsc(Role::getId);
        return lqw;
    }

}
